﻿using System;
using System.Net;
using System.Windows;
using System.IO;
namespace VAdvantageSvc
{
    public class CurrencyConversion
    {
        public static string GetConvertedCurrencyValue(CurrencyConverter.Currency from, CurrencyConverter.Currency to, string url)
        {

             string response = "";
            //else if (url.ToUpper().Contains("YAHOO.COM"))
            //{
            //    WebClient web = new WebClient();
            //    string newUrl = @"http://finance.yahoo.com/currency-converter/#from=" + from.ToString().ToUpper() + ";to=" + to.ToString().ToUpper() + ";amt=1";
            //    response = web.DownloadString(newUrl);

            //}


            if (url.ToUpper().Contains("WEBSERVICEX.NET"))
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                var _currency = new CurrencyConverter.CurrencyConvertorSoapClient();
                _currency.Open();
                response = _currency.ConversionRate(from, to).ToString();
                return response;
            }



            return response;
        }


        public static string GetConvertedCurrencyValue(string from, string to, string url)
        {
            string response = "";
            if (url.ToUpper().Contains("XE.COM"))
            {
                string newUrl = @"http://www.xe.com/ucc/convert/?Amount=1&From=" + from.ToString() + "&To=" + to.ToString() + "";
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(newUrl);
                HttpWebResponse resp = (HttpWebResponse)webReq.GetResponse();//send sms
                StreamReader responseReader = new StreamReader(resp.GetResponseStream());//read the response 
                response = responseReader.ReadToEnd();//get result   
                response = response.Substring(response.IndexOf("layout pane uccPaneCnr"), 700);
                for (int i = 0; i < 15; i++)
                {
                    response = response.Substring(response.IndexOf(">") + 1);
                }
                response = response.Substring(0, response.IndexOf('&'));
                responseReader.Close();
                resp.Close();
                return response;

            }
            else if (url.ToUpper().Contains("GOOGLE.COM"))
            {
                WebClient web = new WebClient();
                string newUrl = @"http://www.google.com/finance/converter?a=1&from=" + from.ToString().ToUpper() + "&to=" + to.ToString().ToUpper() + "";
                response = web.DownloadString(newUrl);
                if (response.Contains("currency_converter_result"))
                {
                    response = response.Substring(response.IndexOf("currency_converter_result"), 70);
                    response = response.Substring(response.IndexOf(">") + 1);
                    response = response.Substring(response.IndexOf(">") + 1);
                    response = response.Substring(0, response.IndexOf(" "));
                    return response;
                }

            }
                        
            return response;
        }
    }
}
